// Decompiled by DJ v2.3.3.38 Copyright 2000 Atanas Neshkov  Date: 03/11/2007 6:40:45
// Home Page : http//members.fortunecity.com/neshkov/dj.html  - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   ConexionDao.java

package ig.formato.dao;

import ig.formato.dao.abs.DAO;
import ig.formato.dto.ConexionDTO;
import ig.formato.util.IGFormatoException;
import ig.formato.util.TripleDESEncryption;
import java.sql.*;
import java.util.ArrayList;
import java.util.Collection;

public class ConexionDao extends DAO {

    public ConexionDao() {
    }

    public Object buscar(Integer id) throws IGFormatoException {
        try {
            /*connectionIgFormato = */ getConnection();
            prep = connectionIgFormato.prepareStatement("SELECT ID, NOMBRE, IDSGBD, HOST, PUERTO, USUARIO, CLAVE, BASEDEDATOS FROM CONEXION WHERE ID = ?");
            prep.setInt(1, id.intValue());
            resul = prep.executeQuery();
            ConexionDTO dto = null;
            if (resul.next()) {
                dto = new ConexionDTO();
                getDatos(resul, dto);
                dto.setClave(TripleDESEncryption.decrypt(dto.getClave(),
                        "informaciongerencial.com"));
            }
            return dto;
        } catch (ClassNotFoundException ex) {
            throw new IGFormatoException(ex);
        } catch (SQLException ex) {
            throw new IGFormatoException(ex);
        } catch (IGFormatoException ex) {
            throw ex;                        
        } catch (Exception ex) {
            throw new IGFormatoException(ex);
        } finally {
            closeAll();
        }

    }

    public Collection buscarTodos() throws IGFormatoException {
        Collection c = new ArrayList();
        try {
            /*connectionIgFormato = */ getConnection();
            prep = connectionIgFormato.prepareStatement("SELECT ID, NOMBRE, IDSGBD, HOST, PUERTO, USUARIO, CLAVE, BASEDEDATOS FROM CONEXION");
            resul = prep.executeQuery();
            ConexionDTO dto = null;
            boolean add;
            while (resul.next()) {
                add = c.add(getDatos(resul, new ConexionDTO()));
            }
            return c;
        } catch (ClassNotFoundException ex) {
            throw new IGFormatoException(ex);
        } catch (SQLException ex) {
            throw new IGFormatoException(ex);
        } catch (IGFormatoException ex) {
            throw ex;            
        } catch (Exception ex) {
            throw new IGFormatoException(ex);
        } finally {
            closeAll();
        }
    }

    public Integer guardar(Object obj) throws IGFormatoException {
        ConexionDTO dto = (ConexionDTO) obj;
        try {
            /*connectionIgFormato = */ getConnection();
            if (dto.getId() == null) {
                prep = connectionIgFormato.prepareStatement("INSERT INTO CONEXION(NOMBRE, IDSGBD, HOST, PUERTO, USUARIO, CLAVE, BASEDEDATOS,ID) VALUES(?,?,?,?,?,?,?,?)");
                dto.setId(getMaximoId("CONEXION"));
            } else {
                prep = connectionIgFormato.prepareStatement("UPDATE CONEXION SET NOMBRE=?, IDSGBD=?, HOST=?, PUERTO=?, USUARIO=?, CLAVE=?, BASEDEDATOS=? WHERE ID=?");
            }
            dto.setClave(TripleDESEncryption.encrypt(dto.getClave(),"informaciongerencial.com"));
            setDatos(prep, dto,"NOMBRE, IDSGBD, HOST, PUERTO, USUARIO, CLAVE, BASEDEDATOS,ID");
            prep.executeUpdate();
            return dto.getId();
        } catch (ClassNotFoundException ex) {
            throw new IGFormatoException(ex);
        } catch (SQLException ex) {
            throw new IGFormatoException(ex);
        } catch (IGFormatoException ex) {
            throw ex;            
        } catch (Exception ex) {
            throw new IGFormatoException(ex);
        } catch (Throwable ex) {
            throw new IGFormatoException(ex);            
        } finally {
            closeAll();
        }
    }

    public void eliminar(Integer id) throws IGFormatoException {
        try {
            /*connectionIgFormato = */ getConnection();
            prep = connectionIgFormato.prepareStatement("DELETE FROM CONEXION WHERE ID = ?");
            prep.setInt(1, id.intValue());
            prep.executeUpdate();
        } catch (SQLException ex) {
            throw new IGFormatoException(ex);
        } catch (IGFormatoException ex) {
            throw ex;            
        } catch (Exception ex) {
            throw new IGFormatoException(ex);
        } finally {
            closeAll();
        }
    }

    public Object existe(Object obj) throws IGFormatoException {
        try {
            /*connectionIgFormato = */ getConnection();
            prep = connectionIgFormato.prepareStatement("SELECT ID FROM CONEXION WHERE NOMBRE = ?");
            prep.setString(1, ((ConexionDTO) obj).getNombre().trim());
            resul = prep.executeQuery();
            if (resul.next()) {
                return Integer.valueOf(resul.getInt("ID"));
            }
        } catch (SQLException ex) {
            throw new IGFormatoException(ex);
        } catch (IGFormatoException ex) {
            throw ex;            
        } catch (Exception ex) {
            throw new IGFormatoException(ex);
        } finally {
            closeAll();
        }
        return false;
    }
}
